"""

第一种通过format方法进行拼接
https://www.baidu.com/s?wd=%E6%B3%95%E6%8B%89%E5%88%A9&rsv_spt=1&rsv_iqid=0
第二种 get方法 的专属表单构建 参数  params  相当于构建http协议的主体信息
"""
import requests
from fake_useragent import FakeUserAgent

wd=input("请输入需要需要爬取的网页:")

url=f"https://www.baidu.com/s?wd={wd}&rsv_spt=1&rsv_iqid=0"
# 构建请求头
user_agent=FakeUserAgent().random

headers={
    "User-Agent": user_agent,
    "Cookie": "BD_UPN=12314753; sug=3; ORIGIN=0; sugstore=0; bdime=0; PSTM=1647264351; BAIDUID=9CE7556020BF8320993AF17CEC6B290F:FG=1; BIDUPSID=CBBA598AB2CF72004A27DF77A269EBEF; BDUSS=nNDdE1lMHR1LXFmaXdxeWN3YTVjVGY4QnpzMjJNRTc1UzdrdWpEQVRRb2FybGRpRVFBQUFBJCQAAAAAAAAAAAEAAABZilOrAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABohMGIaITBiM; BDUSS_BFESS=nNDdE1lMHR1LXFmaXdxeWN3YTVjVGY4QnpzMjJNRTc1UzdrdWpEQVRRb2FybGRpRVFBQUFBJCQAAAAAAAAAAAEAAABZilOrAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABohMGIaITBiM; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; baikeVisitId=7da881df-958a-4110-b22a-95d7226ea2b2; H_PS_645EC=74abJ0DD%2B8bplUYmB9%2Ba%2F7X%2BPKQVrgZdwHhlfN3iheh9mivO6k9RX9scEhc72x2sdshA2ogqzHF8; COOKIE_SESSION=124_0_4_9_0_5_1_0_4_5_0_0_0_0_0_0_0_0_1647610107%7C9%2379991_44_1647340846%7C8; BCLID_BFESS=8245448120837836413; BDSFRCVID_BFESS=mKIOJexroG0xtb3Db0WNKknzQmKK0gOTDYrEOwXPsp3LGJLVg61QEG0PtsxhkZu-oxspogKKXgOTHw0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=tRk8oIPaJDvDqTrP-trf5DCShUFs2xnAB2Q-XPoO3KJWfb_RQ-K23UCUMUTEQ4riWbRM2Mbgy4op8P3y0bb2DUA1y4vpK5KDLmTxoUJ2--nsjUcGqtnWKn8ebPRiXPb9QgbfopQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHj--ejjX3f; BDRCVFR[aGQGCXkfc5Y]=mk3SLVN4HKm; BD_HOME=1; H_PS_PSSID=; BA_HECTOR=0184ah8l2l8l8420bj1h392ko0r"
}
# 发送请求
response=requests.get(url=url,headers=headers)

print(response.content.decode())
with open(f"{wd}.html","wb")as f:
    # 以字节形式进行写入
    f.write(response.content)

#
# 第二种url传参参数的 方法   关键字
# https://www.baidu.com/s?wd=%E6%B3%95%E6%8B%89%E5%88%A9&rsv_spt=1&rsv_iqid=0
# 如何使用parasa参数    这是构建http的一种规范
# wd=%E6%B3%95%E6%8B%89%E5%88%A9&rsv_spt=1&rsv_iqid=0  看到有xx=xx字段的时候就是构建params参数的位置

data=input("请输入需要爬取的内容:")
url="https://www.baidu.com/s?"
headers={
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36",
}
# params是字典类型的数据
params={
    "wd":data,
    "rsv_spt":1,
    "rsv_iqid":0
}
# 发送请求
response=requests.get(url=url,headers=headers,params=params)
text=response.content.decode()
with open(f"{data}.html","wb")as fi1:
    fi1.write(response.content)








